/******************************************************************************************************
Copyright (C) 2008 Mestrelab Research S.L. All rights reserved.

This file is part of the MNova scripting toolkit.

Authorized users of MNova Software may use this file freely, but this file is provided AS IS
with NO WARRANTY OF ANY KIND, INCLUDING THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE.
*****************************************************************************************************/ 

var MyMultipletReporterSettingsKey = new String("MyMultipletReporter/Custom Message");

/* This creates a Custom Multiplet Reporter  */
function MyMultipletReporter()
{
	MultipletReporter.call(this);
	
	var setts=new Settings(Application.settings);
	this.customMessage = setts.value( MyMultipletReporterSettingsKey, "My Multiplet Reporter" );
	
	this.name = "My Multiplet Reporter";
	
	this.onlyElementName=false;
// Define font size and font family
	this.font = "<font style=\"font-size: 10pt; font-family: Times New Roman\">"; 
		
	this.nucleusTemplate="%1";
// Report header. %1 will be raplaced with nucleusString, %2  with frequency, %3 with solvent 
	this.header = this.customMessage+" %1 NMR (%2 MHz, %3) &delta; ";
	//this.header = "Custom %1 NMR (%2 MHz, %3) &delta; ";
		
// Multiplet templates. %1 - delta, %2 - category, %3 - nH
	this.reportRange = true; // set to true to get multiplet range instead of delta.
	this.withoutJsTemplate = " %1 (%2, %3H)"; // multiplet template without J's
	this.withJsTemplate = " %1 (%2, %4, %3H)"; //  multiplet template with J's
	this.rangeTemplate = "%1 &ndash; %2";
		
// J's list template. %1 - list of J's
	this.jListTemplate = "<i>J</i> = %1";  
		
	this.jPrecision = 1; // J's precision
	this.deltaPrecision = 2; // delta precision
	this.mSeparator = ", "; // multiplet separator 
	this.jSeparator = ", "; // J's separator	
	this.sortJAscending=true;		
	this.start = "<html><head><title>Multiplet Report</title></head><body>" + this.font;
	this.end = ".</font></body></html>";
}

MyMultipletReporter.prototype = new MultipletReporter();
MyMultipletReporter.prototype.toString = function() { return "MyMultipletReporter()"; }

//This line adds my custom reporter to the list of reporters, this means that this reporter can be used by the general functions like reportMultipletsUser or automaticMultipletReport and it can be used to generate reports  from the Multiplets Table
Env.MultipletReportersArray.push(new MyMultipletReporter());

//Uncomment this line to make this reporter the default reporter, the saved settings will be ignored, the user will not be able to modify the reporter from MNova
//Env.DefaultMultipletReporter = "My Multiplet Reporter";


//This function can be used to generate multiplet reports
function customReportMultipletsUser(createItem)
{
	var multipletTable = new MultipletTable(nmr.multipletTable()); // Get table of multiplets
	var spectrum;
	if(multipletTable.isValid())
		spectrum = new NMRSpectrum(multipletTable.spectrum()); // Get spectrum currently shown in the table of multiplets
	else
		spectrum = new NMRSpectrum(nmr.activeSpectrum());
	
	if( spectrum.isValid() )
	{
		var reporter = MultipletReporter.getReporterByName("My Multiplet Reporter");
		return reporter.report(spectrum, createItem);
	}
	else
		return undefined;
}

//Uncomment this line to use the previous function to report multiplets instead of the default one
//Env.ReportMultipletsUser = customReportMultipletsUser;

//Function to setup my reporter
function customReportMultipletsUserSetup()
{
	var rep = MultipletReporter.getReporterByName("My Multiplet Reporter");
	
	var diag = new Dialog();
	diag.title = "Custom Setup";
	
	var lEdit = new LineEdit();
	lEdit.text = rep.customMessage;
	diag.add(lEdit);
	
	if(diag.exec())
	{
		rep.customMessage = lEdit.text;
		settings.setValue( MyMultipletReporterSettingsKey, rep.customMessage );
	}
}

//Uncomment this line to use the previous function to setup the report multiplets
//Env.ReportMultipletsUserSetup = customReportMultipletsUserSetup;
